package states

import (
	"go_server/game_server/dao/count_down_dao"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/zinx/ziface"
	"go_server/zinx/znet"
)

type SaveNewGuideApi struct {
	znet.BaseRouter
}

// 保存新手引导进度
func (*SaveNewGuideApi) Handle(request ziface.IRequest) {
	msg := &pb.SaveNewGuideC2S{}
	if !util.PBParseRet(request, msg) {
		return
	}

	conn := request.GetConnection()
	roleId := ServiceObj.GetRoleId(request)

	fields := []string{"guide_list"}
	row := count_down_dao.GetCountDownByRoleId(roleId, fields)

	guideList := make([]int32, 0)
	if row != nil && row.GuideList != nil {
		guideList = row.GuideList
	}

	var update = true

	if len(guideList) > 0 {
		eventSet := util.NewSet[int32]()
		eventSet.AddValue(guideList)
		if !eventSet.Contains(msg.GuideId) { // 没有完成过
			guideList = append(guideList, msg.GuideId)
		} else {
			update = false
		}
	} else {
		guideList = append(guideList, msg.GuideId)
	}

	res := &pb.SaveNewGuideS2C{
		GuideList: guideList,
	}

	// 优先返回下行
	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_SaveNewGuide), res)

	if update {
		countDown := count_down_dao.CountDown{
			RoleId:    roleId,
			GuideList: guideList,
		}
		count_down_dao.UpdateCountDown(&countDown, fields)
	}
}
